package com.gomore.opple.module.award.unlottery;

import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.model.EventReflashLotteriedFragment;
import com.gomore.opple.rest.common.DataPage;
import com.gomore.opple.rest.jdecard.GroupByOrderNumber;
import com.gomore.opple.rest.jdecard.RsJingDongECardRequest;
import com.gomore.opple.rest.jdecard.RsJingDongECardResponse;
import com.gomore.opple.rest.lottery.Draw;
import com.gomore.opple.rest.lottery.RsAwardDraw;
import com.gomore.opple.rest.lottery.RsLotteryRequest;
import com.gomore.opple.rest.lottery.RsLotteryResponse;
import com.gomore.opple.web.cgform.lottery.entity.TODrawEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class UnlotteryPresenter implements UnlotteryContract.Presenter {

    private DataRepository mDataRepositroy;
    private final UnlotteryContract.View mView;
    private CompositeSubscription mSubscriptions;
    private List<RsAwardDraw> rsAwardDrawList = new ArrayList<>();
    private int rows = GlobalConstant.PAGE_SIZE;
    private int page = GlobalConstant.PAGE_START;

    @Inject
    UnlotteryPresenter(DataRepository dataRepository, UnlotteryContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {

    }

    @Override
    public void queryAward(final boolean isLoadMore) {
        if (isLoadMore) {
            page++;
        } else {
            rsAwardDrawList.clear();
            page = GlobalConstant.PAGE_START;
        }
        mView.showProgressDialog();
        RsLotteryRequest rsLotteryRequest = new RsLotteryRequest();
        rsLotteryRequest.setStateEquals(GlobalConstant.AwardState.INITIAL);
        String employid = mDataRepositroy.getUser().getEmployee().getId();
        rsLotteryRequest.setWinnerIdEquals(employid);
        DataPage dataPage = new DataPage();
        dataPage.setRows(rows);
        dataPage.setPage(page);
        rsLotteryRequest.setPage(dataPage);
        mSubscriptions.clear();
        Subscription subscription = mDataRepositroy.queryAward(rsLotteryRequest)
                .map(new ServerResponseFunc<RsLotteryResponse>())
                .onErrorResumeNext(new HttpResponseFunc<RsLotteryResponse>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<RsLotteryResponse>() {
                    @Override
                    public void onNext(RsLotteryResponse rsLotteryResponse) {
                        super.onNext(rsLotteryResponse);
                        mView.hideProgressDialog();
                        if (isLoadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                        if (page <= rsLotteryResponse.getTotalpages()) {
                            rsAwardDrawList.addAll(rsLotteryResponse.getList());
                        }
                        mView.showContentView();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        if (isLoadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public List<RsAwardDraw> getData() {
        return rsAwardDrawList;
    }

}

